﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.TownshipEnterprise.BLL;
using Yipusoft.TownshipEnterprise.Model;
using System.Collections.Generic;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;

public partial class TownshipEnterprise_TownBasicList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.DptDepartment.SelectedIndexChanged += new EventHandler(DptDepartment_SelectedIndexChanged);

        if (!IsPostBack)
            Bind();

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        //this.GvwList.PageIndex = PageIndex;
        Bind2(PageIndex);
    }
    /// <summary>
    /// 隶属支队
    /// </summary>
    private void Bind()
    {
        this.DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,2)='00'");



        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");

        }
        else
        {
            DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");

        }

        this.DptDepartment.DataTextField = "AliasName";
        this.DptDepartment.DataValueField = "DepartmentID";
        this.DptDepartment.DataBind();
        //this.DptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,3)='000' and DepartmentID<>'1300000'");
        //this.DptDepartment.DataTextField = "AliasName";
        //this.DptDepartment.DataValueField = "DepartmentID";
        //this.DptDepartment.DataBind();
        DptDepartment.Items.Insert(0, new ListItem("请选择单位", "00"));
    }
    //查询
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        Bind2(0);
    }
    /// <summary>
    /// 查询
    /// </summary>
    private void Bind2(int pageindex)
    {
        string strSql = string.Empty;
        if (txtName.Text.Trim() != "")
            strSql = " army Like'%" + txtName.Text.Trim() + "%'";

        if (DptDepartment.SelectedValue.Equals("00"))
        {
            goto thisline;
        }


        if (DptDepartment.SelectedValue.Equals("1300000"))
        {
            if (strSql == string.Empty)
            {
                strSql = " 1=1 Order By DepartmentID";
                goto thisline;
            }
            else
            {
                strSql += " Order By DepartmentID";
                goto thisline;
            }
        }
        if (strSql != string.Empty)
        {
            if (!DptDepartment.SelectedValue.Equals("00"))
            {

                strSql += " And Left(DepartmentID,4) = '" + DptDepartment.SelectedValue.Substring(0, 4) + "'";
            }
        }
        else
        {
            if (!DptDepartment.SelectedValue.Equals("00"))
            {
                strSql = "  Left(DepartmentID,4) = '" + DptDepartment.SelectedValue.Substring(0, 4) + "'";
            }
        }

    thisline:
        List<TownBasicInfo> list = TownBasic.GetList(strSql, ListPageContral1.PageSize, pageindex);
        if (list != null)
        {
            this.ListPageContral1.ItemCount = TownBasic.GetCount(strSql);
            this.GvwList.DataSource = list;
            this.GvwList.DataBind();
        }

    }

    public string GetDepartmentType(object objID)
    {
        return objID.ToString();

    }
    public string GetStationLevel(object objID)
    {
        return objID.ToString();

        //if (objID == null)
        //    return "";
        //Yipusoft.Common.Model.CodeItemInfo info = Yipusoft.Common.BLL.CodeItem.GetCodeItem(objID.ToString(), "XFZDJ");
        //return info != null ? info.ItemName : "";
    }
    public string GetStationType(object objID)
    {
        return objID.ToString();
        //if (objID == null)
        //    return "";
        //Yipusoft.Common.Model.CodeItemInfo info = Yipusoft.Common.BLL.CodeItem.GetCodeItem(objID.ToString(), "XFZLB");
        //return info != null ? info.ItemName : "";

    }
    protected void GvwList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(GvwList.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");


            e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "return confirm('确定删除吗？');");
        }
    }
    protected void GvwList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int num = Convert.ToInt32(e.CommandArgument);//得行号
            Guid id = new Guid(this.GvwList.DataKeys[num]["Id"].ToString());//得主键值         

            string s = e.CommandName.ToString();
            //删除
            if (s == "Delete")
            {
                if (TownBasic.Delete(id))
                {
                    Response.Write("<script language=javascript>alert(' 删除成功!')</script>");
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language=javascript>alert('操作失败！gridview-rowcommand错误！" + ex.Message + "！')</script>");
        }
    }
    protected void GvwList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind2(ListPageContral1.CurrentPageIndex);
    }
}
